<?xml version="1.0"?>
<!--
This file is part of the Foothill College Web GIS.

Foothill College Web GIS is free software: you can redistribute it 
and/or modify it under the terms of the GNU General Public License 
as published by the Free Software Foundation, either version 3 of 
the License, or (at your option) any later version.

Foothill College Web GIS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foothill College Web GIS. If not, see 
<http://www.gnu.org/licenses/>.
-->

<!-- run the generated kml through this to add
     id attributes to each placemark. If the KML format
     changes, you'll need to mess with the XPath expression in 
     xsl:attribute 
     cammand: xsltproc -o geocode.kml geocode.xsl src.kml -->

<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:kml="http://www.opengis.net/kml/2.2">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="utf-8"/>


  <!-- everything (except what we modify below) should be unmodified -->
  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <!-- get rid of style information -->
  <xsl:template match="kml:Style" />

  <!-- stick in the style info in the header -->
  <xsl:template match="kml:Document">
    <xsl:copy>
      <!-- here are all the styles -->
      <!-- catchall for unassociated rooms (are there any?) -->
      <Style id="roomStyle">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <!-- be aware: format is aabbggrr (i.e. backwards) -->
	  <color>ffaaaaaa</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 1000 buildings -->
      <Style id="1000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffb0d9cd</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 2000 buildings -->
      <Style id="2000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffd6f6fc</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 3000 buildings -->
      <Style id="3000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffc9b4c9</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 4000 buildings -->
      <Style id="4000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffbcbcee</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 5000 buildings -->
      <Style id="5000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffddc2cb</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 6000 buildings -->
      <Style id="6000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffc8e1f0</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 7000 buildings -->
      <Style id="7000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffaad8f2</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>

      <!-- 8000 buildings -->
      <Style id="8000Style">
	<LineStyle>
	  <color>ff000000</color>
	  <colorMode>normal</colorMode>
	  <width>0.4</width>
	</LineStyle>
	<PolyStyle>
	  <color>ffe6c9b8</color>

	  <!-- not sure it's needed -->
	  <colorMode>normal</colorMode>
	  <fill>1</fill>
	  <outline>1</outline>
	</PolyStyle>
      </Style>
      
      <!-- and stick everything else back in -->
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="kml:Placemark">
    <xsl:copy>
      <!-- Add a styleURL based on building number (actually based on the
           first digit of the room number. Will break if we go to 10000 
	   buildings, as will the geocoder, which also assumes 4-digit 
	   room numbers)-->
      <xsl:choose>
	<!-- 1000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 1)">
	  <styleUrl>#1000Style</styleUrl>
	</xsl:when>

	<!-- 2000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 2)">
	  <styleUrl>#2000Style</styleUrl>
	</xsl:when>

	<!-- 3000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 3)">
	  <styleUrl>#3000Style</styleUrl>
	</xsl:when>

	<!-- 4000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 4)">
	  <styleUrl>#4000Style</styleUrl>
	</xsl:when>

	<!-- 5000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 5)">
	  <styleUrl>#5000Style</styleUrl>
	</xsl:when>

	<!-- 6000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 6)">
	  <styleUrl>#6000Style</styleUrl>
	</xsl:when>

	<!-- 7000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 7)">
	  <styleUrl>#7000Style</styleUrl>
	</xsl:when>

	<!-- 8000 buildings -->
	<xsl:when test="starts-with(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no'], 8)">
	  <styleUrl>#8000Style</styleUrl>
	</xsl:when>

	<xsl:otherwise>
	  <styleUrl>#roomStyle</styleUrl>
	</xsl:otherwise>
      </xsl:choose>
      
      <!-- these should be rendered into the info windows -->
      <description>
	<div class="fhWebMap_infoWindow">
	  <!-- Google and jQuery conspire to break fhWebMap if we put in br
	       tags, so use display: block. Basically, Google incorrectly
	       converts <br/> into <br></br></br>, which jQuery further mangles
	       to <br><br><br>.
	  -->
	  <b style="display: block">
	    <xsl:value-of select="./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='room_no']" /> 
	    <xsl:if test="not(./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='canname_en'] = '')">
	      (<xsl:value-of select="./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='canname_en']" />)
	    </xsl:if>
	  </b>
	  <span style="display: block">
	    <xsl:choose>
	      <xsl:when test="./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='bldglevel']='1'">
		First floor
	      </xsl:when>
	      <xsl:when test="./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='bldglevel']='2'">
		Second floor
	      </xsl:when>
	      <xsl:when test="./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='bldglevel']='3'">
		Third floor
	      </xsl:when>
	    </xsl:choose>
	  </span>
	  <xsl:value-of select="./kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='msg_en']" />
	</div>
      </description>

      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>